// Peer effects in the REDUX/High-3 decision amongst Navy personnel.
cd "/Users/jessecunha/Dropbox/NPS/Retirement peer effects/ReStat Pub Pckge"

// DMDC data on all AD service members, Redux/High-3 choice available for those who have retired
	use admaster_0012_aggregate_rev.dta, clear
	append using admaster_1319_aggregate_rev.dta

// Drop unused variables
	drop dob birth_state birth_cntry faith citiz_status educ_tier educ_disc hor_state hor_cntry legres_state jnt_spuse_grade jnt_spouse_source pay_base_dt enl_career_status rank_date pay_grade_mod rank_months_qy dty_svc_occupation sec_svc_occupation sec_dod_occupation	
	drop ad_svc_years_qy act_fed_base_dt
	drop asgn_uic_state-asgn_uic_maj  duty_uic-duty_uic_bodywater

// Fill in AFQT with the panel max if ever missing
	destring afqt_percentile, replace force
	bys ssnscr: egen afqt_max = max(afqt_percentile)
	replace afqt_percentile = afqt_max
	
// Drop obs before the decision date at 14.5 YOS
	destring ad_svc_months_qy, replace force
	keep if ad_svc_months_qy>=173

// File year/month and AD start year/month
	gen startyear=substr(unif_init_entry_dt, 1, 4)
	gen startmonth=substr(unif_init_entry_dt, 5, 2)
	destring startyear, replace force
	destring startmonth, replace force

	gen fileyear=substr(file_date, 1,4)
	gen filemonth=substr(file_date, 5,2)
	destring fileyear, replace force
	destring filemonth, replace force

// Drop if tenure is undefined	
	drop if ad_svc_months_qy==999|ad_svc_months_qy==.

save temp, replace

// Merge edipi - ssnscr crosswalk
	import excel using DRS_47116_Cunha_EDIPI-SSNSCR.xlsx, firstrow clear sheet("DRS_47116_SC")
	rename *, lower
	drop obsid
	tostring ssnscr, replace

save drs_47116_sc.dta, replace

	use npc_updated_raw.dta, clear
	keep edipi redux_choice redux_date tsp_code tsp_status 
	renvars redux_choice redux_date tsp_code tsp_status, prefix(npc_) // rename to keep separate from DMDC data
	drop if npc_redux_choice=="Choice" // 0.12% of people (61 total) are labeled "Choice"
	g npc_redux=(npc_redux_choice=="REDUX") 
	g npc_high3=(inlist(npc_redux_choice,"Default High-3","High-3")) 
	merge 1:1 edipi using drs_47116_sc.dta
	drop if _m==1 // 4 observations
	drop _m edipi

save temp2, replace

// Format DMDC retirement file which identified the REDUX/HIGH-3 decision
	use "retirement_aggregate.dta", clear 
		gen fileyear=substr(file_date, 1,4)
		destring fileyear, replace force
		drop if inrange(fileyear, 2001, 2008)
	append using "retirement_aggregate_0008rev.dta"
		destring mil_retpay_method, replace
	// Note: 290 people (.16%) have more than one mil_retpay_method
	collapse (min) fileyear, by(ssnscr mil_retpay_method)
		duplicates tag ssnscr, g(DMDC_ret_pay_inconsistent)
	// Keep most recent year
	collapse (firstnm) mil_retpay_method (max) fileyear, by(ssnscr) 

	gen redux=(mil_retpay_method==4)
	gen high3=(mil_retpay_method==3|mil_retpay_method==2)

	drop mil_retpay_method fileyear

save "retirement_sample_rev.dta", replace
	
// Merge NPC data into DMDC data. NPC data contains Redux/High-3 choice for all service members, even those between 14.5 YOS & retirement
	use temp, clear
	merge m:1 ssnscr using temp2
	drop if _m==2 // 2,638 people in NPC data who are not in the DMDC database
	drop _m
		
// Merge in DMDC retirement file
	merge m:1 ssnscr using retirement_sample_rev, gen(_m_retire_file)
	drop if _m_retire_file==2	// ~18k obs in the retirement file who aren't in the DMDC master file
	
	
// Fill in DMDC redux choice with NPC choice and overwrite if disagreement
	replace redux = npc_redux if npc_redux!=.
	replace high3 = npc_high3 if npc_high3!=.
	
// Service characteristics
	g length=strlen(assigned_uic)
	drop if length !=6 // 3 obs
	drop length
	gen uic=substr(assigned_uic, 2, 5)

	gen rank=substr(pay_grade, 2, 3)
	g rank_e5_below = inlist(rank,"E00","E01","E02","E03","E04","E05")
	g rank_e6 = rank=="E06"
	g rank_e7_above = inlist(rank,"E07","E08","E09")
	g rank_o3_below = inlist(rank,"O01","O02","O03","W02","W03")
	g rank_o4_above = inlist(rank,"O04","O05","O06")
	gen officer=(substr(rank, 1, 1)=="O"|substr(rank, 1, 1)=="W")
	gen enlisted=1-officer
	g rank_group = "e0-e5" if rank_e5_below==1
		replace rank_group = "e6" if rank_e6==1
		replace rank_group = "e7_above" if rank_e7_above==1
		replace rank_group = "o1-o3" if rank_o3_below==1
		replace rank_group = "o4-o6" if rank_o4_above==1

	replace afqt_percentile=. if officer==1 // even though some may have it as prior enlisted
	_pctile afqt_percentile, n(3)	// find tercile break points
	g afqt_low  = inrange(afqt_percentile, 0, `r(r1)' )
	_pctile afqt_percentile, n(3)	// find tercile break points
	g afqt_med  = inrange(afqt_percentile,`r(r1)'+1,`r(r2)')
	g afqt_high = (afqt_low==0 & afqt_med==0 & afqt_percentile!=.)
	g afqt_cat = "Low, 1st tercile" if afqt_low==1
		replace afqt_cat = "Medium, 2nd tercile" if afqt_med==1
		replace afqt_cat = "High, 3rd tercile" if afqt_high==1
		
// Demographics
	g female=(sex=="F")
	g male=1-female

	g married=(marital=="M")
	g divorced=(inlist(marital,"A","D","L"))
	g single=(1-(married+divorced)) // includes widowed and unknown
	g marital_status = "married" if married==1
		replace marital_status = "divorced" if divorced==1
		replace marital_status = "single" if single==1
		
	destring educ_level, replace force
	g educ_hs=(educ_level<=43)|(inlist(educ_level,99,48))
	g educ_college=(inlist(educ_level, 44,45,46,51,52)) 
	g educ_grad=(inlist(educ_level,61,62,63,64,65))
		replace educ_college = 1 if officer==1 & educ_grad!=1	// Some officers either have missing data (educ_level==99) or only have HS; hard code them as college if no grad school
		replace educ_hs = 0 if officer==1
	g education = "high school" if educ_hs==1
		replace education = "college" if educ_college==1
		replace education = "graduate school" if educ_grad==1
	
	destring race, replace force
	g white=race~=.&(race==5)
	g black=race~=.&((race==3)|inrange(race, 101,104)|inrange(race, 108,111)|inrange(race,115,118)|inrange(race, 122,124))
	g asian=race~=.&((race==2)|(race==121))
	g other_r=1-(white+black+asian)
	g hisp=inlist(ethnic, "AK", "AL", "AM", "AN", "AO")
	replace white=white-hisp if white==1
	replace hisp=0 if (asian==1|other_r==1|black==1)
	drop race
	g race = "white" if white==1
		replace race = "black" if black==1
		replace race = "asian" if asian==1
		replace race = "hispanic" if hisp==1
		replace race = "other_r" if other_r==1
	g minority = (inlist(race,"asian","hispanic","other_r"))
	
	destring dependents_qy, replace force
	
// Redux/high-3 decision made at a different uic
	g uic_at_choice = uic if ad_svc_months_qy==173
	bys ssnscr: replace uic_at_choice = uic_at_choice[_n-1] if uic_at_choice==""
	bys ssnscr: replace uic_at_choice = uic_at_choice[_N] if uic_at_choice==""
	g dif_uic = (uic != uic_at_choice)
	g same_uic = 1 - dif_uic

// Rank groups
	g grade=substr(rank,2,2)
		destring grade, replace
	g o4_below=(officer==1 & grade<=4)
	g o5_above=(officer==1 & grade>=5)
	g e6_below=(enlisted==1 & grade<=6)
	g e7_above=(enlisted==1 & grade>=7)
	
// Drop unused variables
	drop sex marital ethnic rethnic pay_grade pri_svc_occupation pri_dod_occupation dty_dod_occupation
	

duplicates report ssnscr file_date // 0.5% of sample is duplicates 
sort ssnscr file_date ad_svc_months
bys ssnscr file_date: keep if _n==1 // keep earliest ad_svc_months

// Mean choosing redux, # choosers, and total # of all peers in UIC past 15 YOS
	bys uic file_date: g peer_tot_cnt = _N if ad_svc_months_qy >=181 // Total # peers over 15 YOS
	bys uic file_date: egen peer_red_cnt = count(redux) if ad_svc_months_qy >=181 // # of peers who have made the redux/high-3 choice
	bys uic file_date: egen peer_mean = mean(redux) if ad_svc_months_qy >=181 // % of choosers electing Redux

	// Fill in for observations below 15 YOS
	foreach i in peer_tot_cnt peer_red_cnt peer_mean {
		bys uic file_date: replace `i' = `i'[_n-1] if `i'==.
		bys uic file_date: replace `i' = `i'[_N] if `i'==.
	}
	
// Summary stats by other characteristics
// Officer/enlisted, race, rate
	foreach i in officer enlisted dif_uic same_uic white black minority ///
			male female rate_aviation rate_deck rate_eng rate_cs rate_support rate_ops rate_construction rate_sof ///
			o4_below o5_above e6_below e7_above {
		bys uic file_date: g peer_tot_cnt_`i' = _N if ad_svc_months_qy >=181 & `i'==1 // Total # peers over 15 YOS
		bys uic file_date: egen peer_red_cnt_`i' = count(redux) if ad_svc_months_qy >=181 & `i'==1 // # of peers who have made the redux/high-3 choice
		bys uic file_date: egen peer_mean_`i' = mean(redux) if ad_svc_months_qy >=181 & `i'==1 // % of choosers electing Redux
	}
	
	// Fill in for observations below 15 YOS
	foreach i in peer_tot_cnt peer_red_cnt peer_mean {
		foreach j in officer enlisted dif_uic same_uic white black minority ///
				male female rate_aviation rate_deck rate_eng rate_cs rate_support rate_ops rate_construction rate_sof ///
				o4_below o5_above e6_below e7_above {
			bys uic file_date: replace `i'_`j' = `i'_`j'[_n-1] if `i'_`j'==.
			bys uic file_date: replace `i'_`j' = `i'_`j'[_N] if `i'_`j'==.
		}
	}


// By race, gender within officer/enlisted
	foreach i in officer enlisted {
		foreach j in dif_uic same_uic white black minority male female {
			bys uic file_date: g peer_tot_cnt_`i'_`j' = _N if ad_svc_months_qy >=181 & `i'==1 & `j'==1 // Total # peers over 15 YOS
			bys uic file_date: egen peer_red_cnt_`i'_`j' = count(redux) if ad_svc_months_qy >=181 & `i'==1 & `j'==1 // # of peers who have made the redux/high-3 choice
			bys uic file_date: egen peer_mean_`i'_`j' = mean(redux) if ad_svc_months_qy >=181 & `i'==1 & `j'==1 // % of choosers electing Redux
		}
	}
	
	// Fill in for observations below 15 YOS
	foreach i in peer_tot_cnt peer_red_cnt peer_mean {
		foreach j in officer enlisted {
			foreach k in close far dif_uic same_uic white black minority male female {
				bys uic file_date: replace `i'_`j'_`k' = `i'_`j'_`k'[_n-1] if `i'_`j'_`k'==.
				bys uic file_date: replace `i'_`j'_`k' = `i'_`j'_`k'[_N] if `i'_`j'_`k'==.
			}
		}
	}
	
// Address problem of missing peer means by subgroup if no peers of that subgroup exist
	foreach i in officer enlisted dif_uic same_uic white black minority ///
			male female rate_aviation rate_deck rate_eng rate_cs rate_support rate_ops rate_construction rate_sof ///
			o4_below o5_above e6_below e7_above {
		g peer_mean_`i'_m = (peer_mean_`i' == .) if peer_mean!=.
		g peer_mean_`i'_r = peer_mean_`i'
			replace peer_mean_`i'_r = 9 if peer_mean_`i'_m==1
	}
	foreach i in officer enlisted {
		foreach j in dif_uic same_uic white black /*hisp asian other_r*/ minority male female {
			g peer_mean_`i'_`j'_m = (peer_mean_`i'_`j' == .) if peer_mean!=.
			g peer_mean_`i'_`j'_r = peer_mean_`i'_`j'
				replace peer_mean_`i'_`j'_r = 9 if peer_mean_`i'_`j'_m==1
		}
	}

// Appendix Figure D.4 - Career survival histogram
	preserve
	keep ssnscr officer rank file_date ad_svc_months_qy
	bys ssnscr: egen max_months=max(ad_svc_months_qy)
	destring file_year, replace
	keep if file_date<=200406	// Most recent date that capture a possible 30 YOS
	g max_years=max_months/12
	tab max_years
	kdensity max_years
	graph export "../Output/fig_career_length.pdf", replace
	restore

	
// Keep only the observations at 173 months of service
	keep if ad_svc_months_qy == 173
	duplicates report ssnscr // a handful have a duplicate, most listing consecutive months. keep most recent
	sort ssnscr file_date
	bys ssnscr: keep if _n==1
	
// Keep only months with complete retirement data
	drop if fileyear>=2012 | (fileyear == 2011 & filemonth == 12) | fileyear<2002 // Complete data starts in Jan 2001 and goes to Nov 2011
// Drop if Redux chioce is missing
	drop if redux==. // About 5% of sample
// Drop if there are no peers who have chosen Redux/high-3 (1.6% of the sample)
	drop if peer_red_cnt==. | peer_red_cnt==0 // missing when there are no peers above 181 months of service; 0 when no people over that tenure have chosen
	
// Regression variables
	egen uic_yr_group=group(uic fileyear)

	g fiscal_year = fileyear if inrange(filemonth,1,9)
		replace fiscal_year = fileyear + 1 if inrange(filemonth,10,12)
		egen uic_fiscal_year_group=group(uic fiscal_year)
		
	egen uic_group=group(uic)
	rename rank_group rank_group_text
	egen rank_group=group(rank)
	egen rating_group=group(rating)

	g all = 1
	
	g dependents_none = (dependents_qy == 0)
	g dependents_1 = (dependents_qy ==1)
	g dependents_2plus = 1 - dependents_none - dependents_1
	
	global ctrls black minority female married dependents_qy educ_grad educ_college afqt_med afqt_high afqt_m i.rank_group rate_*

	// Restrict education for enlisted to college only, no grad school
	replace educ_college = 1 if educ_grad==1 & enlisted==1
	replace educ_grad = 0 if educ_grad==1 & enlisted==1
	replace education = "college" if education=="graduate school" & enlisted==1
	
	//  # of peers
	* remove the singletons from the first quintile
	xtile peer_n_quintile = peer_red_cnt, nquantiles(5)
	xtile peer_n_quintile_officer = peer_red_cnt_officer if officer==1, nquantiles(5)
	xtile peer_n_quintile_enlisted = peer_red_cnt_enlisted if officer==0, nquantiles(5)


	
// Analysis	
global ctrls black minority female married dependents_qy educ_grad educ_college afqt_med afqt_high officer i.rank_group rate_*

// Table 1 - Sum stats
sum redux officer male white black minority single married dependents_qy educ_hs educ_coll educ_grad 				 peer_red_cnt peer_mean, sep(0)
sum redux 		  male white black minority single married dependents_qy educ_hs educ_coll educ_grad				 peer_red_cnt peer_mean if officer==1, sep(0)
sum redux  		  male white black minority single married dependents_qy educ_hs educ_coll educ_grad afqt_percentile peer_red_cnt peer_mean  if enlisted==1, sep(0)

// Table 2 - Balance check
global bal_vars female black minority married dependents_qy educ_college educ_grad
reghdfe peer_mean $bal_vars, a(uic_gr rating_group rank_group fileyear)
	sum peer_mean
reghdfe peer_mean_officer $bal_vars if officer==1, a(uic_gr rating_group rank_group fileyear)
	sum peer_mean_officer
reghdfe peer_mean_enlisted $bal_vars afqt_per if enlisted==1, a(uic_gr rating_group rank_group fileyear)
	sum peer_mean_enlisted

/// Table 3 - Peer influence on Redux desision
areg redux peer_mean, a(uic) cl(uic_yr_group)
areg redux peer_mean, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean rate_*, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean rate_*  i.rank_group, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean $ctrls, a(uic_yr_group) cl(uic_yr_group)

// Table 4 - By officer/enlisted and education 
areg redux peer_mean $ctrls if officer==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean $ctrls if enlisted==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if officer==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if enlisted==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if educ_college==1 & officer==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if educ_grad==1 & officer==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if educ_hs==1 & enlisted==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if (educ_college==1 | educ_grad==1) & enlisted==1, a(uic_yr_group) cl(uic_yr_group)

// Table 5 - by race and gender
foreach i in white black minority {
	areg redux peer_mean $ctrls if `i'==1, a(uic_yr_group) cl(uic_yr_group)
}
foreach i in white black minority {
	areg redux peer_mean_white_* peer_mean_black_* peer_mean_minority_* $ctrls if `i'==1, a(uic_yr_group) cl(uic_yr_group)
}
areg redux peer_mean $ctrls if male==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean $ctrls if female==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_male_r peer_mean_female_r peer_mean_male_m peer_mean_female_m $ctrls if male==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_male_r peer_mean_female_r peer_mean_male_m peer_mean_female_m $ctrls if female==1, a(uic_yr_group) cl(uic_yr_group)


// Appendix Figure D.1 - Distribution of higher-tenure peers in a unit
histogram peer_mean, frac xtitle(Fraction of peers who chose Redux) ytitle(Fraction of unit-year-months)

// Appendix Figure D.2 - fraction of peers who chose Redux
histogram peer_red_cnt, frac xtitle(Number of peers with more thatn 15 YOS) ytitle(Fraction of unit-year-months)

// Appendix Figure D.5 - Effects by year
forval i=2002/2011 {
	areg redux peer_mean $ctrls if fileyear==`i', a(uic_group) cl(uic_group)
}

// Appendix Table D.1 - IV using choosers at different UIC
xtset uic_yr_group
xtivreg redux $ctrls (peer_mean=peer_mean_dif_uic) if peer_mean_same_uic~=., fe vce(cluster uic_yr_group)
xtivreg redux $ctrls (peer_mean_officer = peer_mean_officer_dif_uic) if officer==1&peer_mean_officer_same_uic~=., fe  vce(cluster uic_yr_group)
xtivreg redux  $ctrls  (peer_mean_enlisted = peer_mean_enlisted_dif_uic) if enlisted==1&peer_mean_enlisted_same_uic~=., fe  vce(cluster uic_yr_group)
xtivreg redux  $ctrls  (peer_mean_officer peer_mean_enlisted = peer_mean_officer_dif_uic peer_mean_enlisted_dif_uic) if officer==1&peer_mean_officer_same_uic~=.&peer_mean_enlisted_same_uic~=., fe  vce(cluster uic_yr_group)
xtivreg redux  $ctrls  (peer_mean_officer peer_mean_enlisted = peer_mean_officer_dif_uic peer_mean_enlisted_dif_uic) if enlisted==1&peer_mean_enlisted_same_uic~=.&peer_mean_officer_same_uic~=., fe  vce(cluster uic_yr_group)

// Appendix Table D.2 - within race and officer/enlisted status
foreach j in officer enlisted {
foreach i in white black minority {
	areg redux peer_mean_*_white_* peer_mean_*_black_* peer_mean_*_minority_*  $ctrls if `i'==1 & `j'==1, a(uic_yr_group) cl(uic_yr_group)
}
}

// Appendix Table D.3 - withing gender and officer/enlisted status
foreach j in officer enlisted {
foreach i in male female {
	areg redux peer_mean_*_male_* peer_mean_*_female_* $ctrls if `i'==1 & `j'==1, a(uic_yr_group) cl(uic_yr_group)
}
}

// Appendix Table D.4
foreach i in rate_aviation rate_deck rate_eng rate_cs rate_support rate_ops rate_construction rate_sof {
	areg redux peer_mean_rate_*_r peer_mean_rate_*_m $ctrls if `i'==1, a(uic_yr_group) cl(uic_yr_group)
}

// Appendix Table D.5 - by size of peer group
forval i=1/5 {
	areg redux peer_mean $ctrls if peer_n_quintile==`i', a(uic_yr_group) cl(uic_yr_group)
}
bys peer_n_quintile: sum peer_red_cnt

// Appendix Table D.6 - by size of peer group and officer/enlisted
foreach j in officer enlisted {
	forval i=1/5 {	
		areg redux peer_mean_officer_r peer_mean_officer_m peer_mean_enlisted_r peer_mean_enlisted_m $ctrls if peer_n_quintile_`j'==`i' & `j'==1, a(uic_yr_group) cl(uic_yr_group)
	}
}
bys peer_n_quintile_officer: sum peer_red_cnt_officer
bys peer_n_quintile_enlisted: sum peer_red_cnt_enlisted

// Footnote 11 - % of choices made at dif UIC
g pct_dif_uic = peer_red_cnt_dif_uic / peer_red_cnt
sum pct_dif_uic, d
histogram pct_dif_uic

// Robustness check - exclude when any peer group is missing
areg redux peer_mean_officer peer_mean_enlisted $ctrls if officer==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_officer peer_mean_enlisted $ctrls if enlisted==1, a(uic_yr_group) cl(uic_yr_group)
foreach i in white black minority {
	areg redux peer_mean_white peer_mean_black peer_mean_minority $ctrls if `i'==1, a(uic_yr_group) cl(uic_yr_group)
}
areg redux peer_mean_male peer_mean_female $ctrls if male==1, a(uic_yr_group) cl(uic_yr_group)
areg redux peer_mean_male peer_mean_female $ctrls if female==1, a(uic_yr_group) cl(uic_yr_group)
foreach i in rate_aviation rate_deck rate_eng rate_cs rate_support rate_ops rate_construction rate_sof {
	areg redux peer_mean_rate_aviation peer_mean_rate_deck peer_mean_rate_eng peer_mean_rate_cs peer_mean_rate_support peer_mean_rate_ops peer_mean_rate_construction peer_mean_rate_sof $ctrls if `i'==1, a(uic_yr_group) cl(uic_yr_group)
}
